import 'package:chat/common/widgets/FriendGroupSection.dart';
import 'package:chat/common/widgets/HomeMenu.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'index.dart';

class FriendPage extends GetView<FriendController> {
  const FriendPage({super.key});

  // 主视图
  Widget _buildView(FriendController controller) {
    return Obx(() {
      if (controller.groupedFriends.keys.toList().isEmpty) {
        return const Center(child: Text("暂无好友"));
      }

      return ListView(
        children: [
          // 固定入口：新的朋友
          Container(
            color: Colors.white,
            child: ListTile(
              title: const Text("新的朋友"),
              onTap: () => EasyLoading.showToast("跳转到新的朋友"),
              leading: const CircleAvatar(backgroundColor: Colors.white, child: Icon(Icons.person_add, color: Colors.blue, size: 35)),
            ),
          ),
          Divider(color: Colors.grey.shade300, height: 0.5, thickness: 0.5, indent: 70, endIndent: 0),
          // 固定入口：群聊
          Container(
            color: Colors.white,
            child: ListTile(
              leading: const CircleAvatar(backgroundColor: Colors.white, child: Icon(Icons.group, color: Colors.green, size: 35)),
              title: const Text('群聊'),
              onTap: () => EasyLoading.showToast("跳转到群聊"),
            ),
          ),
          const SizedBox(height: 10),

          // 好友分组
          ...controller.groupedFriends.keys.toList().map((group) {
            return FriendGroupSection(group: group, friends: controller.groupedFriends[group]!);
          }),
        ],
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<FriendController>(
      init: FriendController(),
      builder: (controller) {
        return Scaffold(
          appBar: AppBar(title: const Text("好友"), backgroundColor: Colors.white, centerTitle: true, actions: [HomeMenu()]),
          body: SafeArea(child: _buildView(controller)),
        );
      },
    );
  }
}
